// pages/car/car.js
var {getHomeGoods} = require("../../api/Home");
Page({

  /**
   * 页面的初始数据
   */
  data: {
      goodsList:[],
      totalData:0,
      likeList:[]
  },
  gohome(){
    wx.switchTab({
        url: '/pages/home/home',
    })
  },
  goPay(){
    wx.navigateTo({
      url: '/pages/pay/pay',
    })
},
  // 数量加一
  add(options){
      console.log(options.target.dataset.id);
      let index = options.target.dataset.id;
      console.log(this.data.goodsList);
      let num = this.data.goodsList[index].num;
      let key = "goodsList["+index+"].num"
      // 写法一
      // let obj = {};
      // obj[key] = num+1;
      // this.setData(obj)
      // 写法二  推荐
      this.setData({
          [key]:num+1
      })

      this.getTotal();
  },
  // 数据减一
  reduce(options){
      
      let index = options.target.dataset.id;
      let num = this.data.goodsList[index].num;
      let key = "goodsList["+index+"].num";
      num = num <= 1? 1: num-1;
      this.setData({
          [key]:num
      })
      
      this.getTotal();
  },

  // 删除数据
  del(options){
      console.log(options);
      let index = options.currentTarget.dataset.id;
      let list = this.data.goodsList;
      list.splice(index,1);
      console.log(list);
      // this中数据删除后重新渲染视图
      this.setData({
          goodsList:list
      }) 
      console.log(this.data.goodsList);
      if(this.data.goodsList.length == 0){
          wx.removeStorageSync('goodslist');
      }    
      this.getTotal();
  },

  // 计算总价格
  getTotal:function(){
      // 获取数据
      let goodsList = this.data.goodsList;
        console.log(goodsList)
        if(goodsList){
            let data = goodsList.reduce(function(total,item){
                return total + item.num *item.goodsPrice;
            },0)
            this.setData({
                totalData:data
            })
        }
      
      
      
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getHomeGoods().then((data)=>{
        let lists = [];
        let goodslists = data.message.goods;
        goodslists.forEach(item=>{
            let {goods_id,goods_small_logo,goods_name,goods_price} = item;
            let data = {
                id:goods_id,
                goodsImage:goods_small_logo,
                goodsName:goods_name,
                goodsAddress:"广州",
                goodsPrice:goods_price
            };
            if(goods_small_logo){
                lists.push(data);
            }
        })
        this.setData({
            likeList:lists
        })
    })
      
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      
      this.setData({
          goodsList:wx.getStorageSync('goodslist')
      })
    //   if(this.data.goodsList = []){
    //     wx.removeStorageSync('goodslist');
    // } 
      this.getTotal();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
      
      if(this.data.goodsList.length != 0){
        wx.setStorageSync('goodslist',this.data.goodsList);
    } 
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})